<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="../common.jsp"%>
<%

    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>编辑</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <base href="<%=basePath%>">
    <link rel="stylesheet" href="${ctx}/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="${ctx}/css/public.css" media="all" />
    <script type="text/javascript" charset="utf-8" src="<%=basePath%>ueditor/ueditor.config.js"></script>
    <script type="text/javascript" charset="utf-8" src="<%=basePath%>ueditor/ueditor.all.min.js"> </script>
    <!--建议手动加在语言，避免在ie下有时因为加载语言失败导致编辑器加载失败-->
    <!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型，比如你在配置项目里配置的是英文，这里加载的中文，那最后就是中文-->
    <script type="text/javascript" charset="utf-8" src="<%=basePath%>ueditor/lang/zh-cn/zh-cn.js"></script>

</head>
<body class="childrenBody">
<form class="layui-form" style="width:90%;">

    <div class="layui-col-md9 layui-col-xs7">
        <div class="layui-form-item magt3">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="hidden" value="${requestScope.demo.id}" name="demoId">
                <input type="text" class="layui-input userName" lay-verify="required" name="userName"  value="${requestScope.demo.userName}" placeholder="请输入用户名">
            </div>
        </div>
    </div>

    <c:if test="${requestScope.admin==null}">
        <div class="layui-col-md9 layui-col-xs7">
            <div class="layui-form-item magt3">
                <label class="layui-form-label">年龄</label>
                <div class="layui-input-block">
                    <input type="number" class="layui-input userAge" lay-verify="required"  name="userAge"  value="${requestScope.demo.userAge}" placeholder="请输入年龄">
                </div>
            </div>
        </div>
    </c:if>

    <div class="layui-col-md9 layui-col-xs7">
        <label class="layui-form-label">选择性别</label>
        <div class="layui-input-block">
            <select name="userSex" class="userSex" lay-filter="required">
                <option value="1" <c:if test="${requestScope.demo.userSex == 1}">selected</c:if> >男</option>
                <option value="2" <c:if test="${requestScope.demo.userSex == 2}">selected</c:if> >女</option>
                <option value="3" <c:if test="${requestScope.demo.userSex == 3}">selected</c:if> >中性</option>
            </select>
        </div>
    </div>

    <div class="layui-col-md9 layui-col-xs7">
        <label class="layui-form-label">内容：</label>
        <div class="layui-input-block">
            <!-- 富文本编辑测试-->
            <script id="editorId" type="text/plain" style="width:1020px;height:618px;">
                ${requestScope.demo.userContent}
            </script>
        </div>
    </div>
    <div class="layui-form-item layui-row layui-col-xs12" style="margin-top: 20px;">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-sm" lay-submit="" lay-filter="sub">立即提交</button>
        </div>
    </div>
</form>
<script type="text/javascript" src="${ctx}/layui/layui.js"></script>
<script>
    //初始化编辑器:
    var ue = UE.getEditor('editorId', { zIndex: 100});
    //页面数据操作
    layui.use(['form','layer','layedit','laydate','upload'],function(){
        var form = layui.form
        layer = parent.layer === undefined ? layui.layer : top.layer,
            laypage = layui.laypage,
            layedit = layui.layedit,
            $ = layui.jquery;

        form.on("submit(sub)",function(data){

            var subData = {};
            //富文本的内容
            var content = ue.getContent();
            subData.userName = data.field.userName;
            subData.userAge = data.field.userAge;
            subData.userSex = data.field.userSex;
            subData.userContent = content;
            var demoId = data.field.demoId;
            if (demoId) {
                subData.id = data.field.demoId;
            }

            //弹出loading
            var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
            $.ajax({
                //json字符串传输参数
                url: '${ctx}/demo/update',
                //实体对象传输参数
                <%--url: '${ctx}/demo/saveDemo',--%>
                contentType:"application/json",
                type: 'POST',
                async: false,
                // data: subData,
                data: JSON.stringify(subData),
                dataType: "json",
                success: function (res) {
                    if (res.code == 10200){
                        top.layer.close(index);
                        top.layer.msg("操作成功！",{icon: 6});
                        parent.location.reload();
                    }else {
                        top.layer.msg(res.message,{icon: 5});
                    }
                }, error: function () {
                    top.layer.msg("系统异常！", {icon: 6});
                    top.layer.close(index);
                }
            });
            return false;
        });
    });
</script>
</body>
</html>
